package com.example.demo.practice.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.demo.practice.R;
import com.example.demo.practice.tool.UnitConversion;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MenuTitle extends LinearLayout {
    private boolean fold = false;
    private MyGridView gridView;
    private ImageView imageView;
    private Context context;

    public MenuTitle(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        UnitConversion unitConversion = new UnitConversion();
        setOrientation(VERTICAL);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MenuTitle);
        String title = typedArray.getString(R.styleable.MenuTitle_menu_title_text);
        //新建子视图

        RelativeLayout frameLayout = new RelativeLayout(context);

        TextView textView = new TextView(context);
        textView.setText(title);
        textView.setTextSize(17);
        textView.setTextColor(Color.WHITE);
        textView.setPadding(30, 0, 0, 0);

        gridView = new MyGridView(context);

        //添加视图
        frameLayout.addView(textView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        addView(frameLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        addView(gridView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        //文字设置
        textView.setGravity(Gravity.CENTER_VERTICAL);
        RelativeLayout.LayoutParams text_layoutParams = (RelativeLayout.LayoutParams) textView.getLayoutParams();
        text_layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
        text_layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        text_layoutParams.width = unitConversion.dpToPixel(100, context);
        text_layoutParams.height = unitConversion.dpToPixel(60, context);


        //网格视图设计
        gridView.setColumnWidth(unitConversion.dpToPixel(80, context));
        gridView.setNumColumns(4);
        gridView.setGravity(Gravity.CENTER);

        typedArray.recycle();
    }

    public void setView(int[] icon, String[] iconName) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < icon.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", icon[i]);
            map.put("text", iconName[i]);
            list.add(map);
        }

        String[] from = {"image", "text"};
        int[] to = {R.id.image, R.id.text};
        SimpleAdapter simpleAdapter = new SimpleAdapter(context, list, R.layout.grid_view_item, from, to);
        gridView.setAdapter(simpleAdapter);
    }

    /**
     * gridView添加子项的点击监听
     *
     * @param listener
     */
    public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
        gridView.setOnItemClickListener(listener);
    }



}
